<?php

/**
 * Subclass for representing a row from the 'site' table.
 *
 * 
 *
 * @package lib.model
 */
class Site extends BaseSite {
	
	public function __toString() {
		return $this->name;
	}
	
	
	
	public function delete(PropelPDO $con = null) {
		///Удаляем папку 
		$this->removeDir ( sfConfig::get ( 'app_cms_dir' ) . $this->name );
		//echo  sfConfig::get ( 'app_cms_dir' ) . $this->name;
		//die;
		parent::delete ();
	}
	
	public function removeDir($dir) {
		if ($objs = glob ( $dir . "/*" )) {
			foreach ( $objs as $obj ) {
				is_dir ( $obj ) ? $this->removeDir ( $obj ) : unlink ( $obj );
			}
		}
		@unlink($dir."/.htaccess"); 
		@rmdir ( $dir );
	}

	
	public function refresh() {
		////Создаем каталоги дистрибутива сайта
		

		$this->removeDir ( sfConfig::get ( 'app_cms_dir' ) . $this->name );
		
		if (! $dir = @opendir ( sfConfig::get ( 'app_cms_dir' ) . $this->name )) {
			
			@mkdir ( sfConfig::get ( 'app_cms_dir' ) . $this->name );
			@mkdir ( sfConfig::get ( 'app_cms_dir' ) . $this->name . '/images' );
			@mkdir ( sfConfig::get ( 'app_cms_dir' ) . $this->name . '/css' );
			@mkdir ( sfConfig::get ( 'app_cms_dir' ) . $this->name . '/js' );
			@mkdir ( sfConfig::get ( 'app_cms_dir' ) . $this->name . '/cache' );
			@mkdir ( sfConfig::get ( 'app_cms_dir' ) . $this->name . '/templates' );
			
			////Копируем php файлы   
			copy ( sfConfig::get ( 'sf_root_dir' ) . '/src/index.php', sfConfig::get ( 'app_cms_dir' ) . $this->name . "/index.php" );
			copy ( sfConfig::get ( 'sf_root_dir' ) . '/src/content_controller.php', sfConfig::get ( 'app_cms_dir' ) . $this->name . "/content_controller.php" );
			copy ( sfConfig::get ( 'sf_root_dir' ) . '/src/menu_controller.php', sfConfig::get ( 'app_cms_dir' ) . $this->name . "/menu_controller.php" );
			copy ( sfConfig::get ( 'sf_root_dir' ) . '/src/cc.php', sfConfig::get ( 'app_cms_dir' ) . $this->name . "/cc.php" );
			copy ( sfConfig::get ( 'sf_root_dir' ) . '/src/contact.php', sfConfig::get ( 'app_cms_dir' ) . $this->name . "/contact.php" );
			copy ( sfConfig::get ( 'sf_root_dir' ) . '/src/xml_parser.php', sfConfig::get ( 'app_cms_dir' ) . $this->name . "/xml_parser.php" );
			
			////Копируем шаблоны   
			copy ( sfConfig::get ( 'sf_root_dir' ) . '/src/templates/portfolio_pager.tpl', sfConfig::get ( 'app_cms_dir' ) . $this->name . "/templates/portfolio_pager.tpl" );
			copy ( sfConfig::get ( 'sf_root_dir' ) . '/src/templates/portfolio_item.tpl', sfConfig::get ( 'app_cms_dir' ) . $this->name . "/templates/portfolio_item.tpl" );
			
			///парсим и копируем распаковщик 
			$filename = sfConfig::get ( 'sf_root_dir' ) . '/src/extract.php';
			$fd = fopen ( $filename, "r" );
			$contents = fread ( $fd, filesize ( $filename ) );
			fclose ( $fd );
			$contents = str_replace ( '@site@', $this->name, $contents );
			$f_t = sfConfig::get ( 'sf_web_dir' ) . '/' . sfConfig::get ( 'app_cms_dir' ) . $this->name . "/extract.php";
			$fd = fopen ( $f_t, "w+" );
			fwrite ( $fd, $contents );
			fclose ( $fd );
			//////////////////////////
			

			///парсим и копируем конфигурационный файл 
			$filename = sfConfig::get ( 'sf_root_dir' ) . '/src/config.php';
			$fd = fopen ( $filename, "r" );
			$contents = fread ( $fd, filesize ( $filename ) );
			fclose ( $fd );
			$contents = str_replace ( '@main_domain@', sfConfig::get('app_main_domain'), $contents );
			$contents = str_replace ( '@name_domain@', $this->name, $contents );
			$contents = str_replace ( '@slogan1@', $this->slogan1, $contents );
			$contents = str_replace ( '@slogan2@', $this->slogan2, $contents );
			$contents = str_replace ( '@text_contact@', $this->text_contact, $contents );
			
			////Меню
			$contents = str_replace ( '@menu_mainpage@', $this->menu_mainpage, $contents );
			$contents = str_replace ( '@menu_service@', $this->menu_service, $contents );
			$contents = str_replace ( '@menu_articles@', $this->menu_articles, $contents );
			$contents = str_replace ( '@menu_contact@', $this->menu_contact, $contents );
			/////
			

			////SEO
			$contents = str_replace ( '@seo_title@', $this->seo_title, $contents );
			$contents = str_replace ( '@seo_description@', $this->seo_description, $contents );
			$contents = str_replace ( '@seo_keywords@', $this->seo_keywords, $contents );
			$contents = str_replace ( '@seo_code_counter@', $this->seo_code_counter, $contents );
			/////
			

			$f_t = sfConfig::get ( 'app_cms_dir' ) . $this->name . "/config.php";
			$fd = fopen ( $f_t, "w+" );
			fwrite ( $fd, $contents );
			fclose ( $fd );
			//////////////////////////
			

			//copy ( sfConfig::get ( 'sf_root_dir' ) . '/src/extract.php', sfConfig::get ( 'app_cms_dir' ) . $this->name . "/extract.php" );
			//////////////////////////////////
			

			////Копируем стилевик   
			copy ( sfConfig::get ( 'sf_root_dir' ) . '/src/css/main.css', sfConfig::get ( 'app_cms_dir' ) . $this->name . "/css/main.css" );
			//////////////////////////////////
			

			////Копируем яваскрипты   
			copy ( sfConfig::get ( 'sf_root_dir' ) . '/src/js/main.js', sfConfig::get ( 'app_cms_dir' ) . $this->name . "/js/js.css" );
			copy ( sfConfig::get ( 'sf_root_dir' ) . '/src/js/fixpng.js', sfConfig::get ( 'app_cms_dir' ) . $this->name . "/js/fixpng.js" );
			
			//////////////////////////////////
			

			////Копируем картинки   
			

			copy ( sfConfig::get ( 'sf_root_dir' ) . '/src/images/home_contact.png', sfConfig::get ( 'app_cms_dir' ) . $this->name . "/images/home_contact.png" );
			copy ( sfConfig::get ( 'sf_root_dir' ) . '/src/images/top_header.jpg', sfConfig::get ( 'app_cms_dir' ) . $this->name . "/images/top_header.jpg" );
			copy ( sfConfig::get ( 'sf_root_dir' ) . '/src/images/logo_wezom.png', sfConfig::get ( 'app_cms_dir' ) . $this->name . "/images/logo_wezom.png" );
			copy ( sfConfig::get ( 'sf_root_dir' ) . '/src/images/menu_back.jpg', sfConfig::get ( 'app_cms_dir' ) . $this->name . "/images/menu_back.jpg" );
			
			copy ( sfConfig::get ( 'sf_root_dir' ) . '/src/images/content_back_bot.gif', sfConfig::get ( 'app_cms_dir' ) . $this->name . "/images/content_back_bot.gif" );
			copy ( sfConfig::get ( 'sf_root_dir' ) . '/src/images/content_back_top.gif', sfConfig::get ( 'app_cms_dir' ) . $this->name . "/images/content_back_top.gif" );
			copy ( sfConfig::get ( 'sf_root_dir' ) . '/src/images/content_back_right.gif', sfConfig::get ( 'app_cms_dir' ) . $this->name . "/images/content_back_right.gif" );
			copy ( sfConfig::get ( 'sf_root_dir' ) . '/src/images/content_back_left.gif', sfConfig::get ( 'app_cms_dir' ) . $this->name . "/images/content_back_left.gif" );
			copy ( sfConfig::get ( 'sf_root_dir' ) . '/src/images/content_bot_left.gif', sfConfig::get ( 'app_cms_dir' ) . $this->name . "/images/content_bot_left.gif" );
			copy ( sfConfig::get ( 'sf_root_dir' ) . '/src/images/content_bot_right.gif', sfConfig::get ( 'app_cms_dir' ) . $this->name . "/images/content_bot_right.gif" );
			copy ( sfConfig::get ( 'sf_root_dir' ) . '/src/images/content_top_right.gif', sfConfig::get ( 'app_cms_dir' ) . $this->name . "/images/content_top_right.gif" );
			copy ( sfConfig::get ( 'sf_root_dir' ) . '/src/images/content_top_left.gif', sfConfig::get ( 'app_cms_dir' ) . $this->name . "/images/content_top_left.gif" );
			//////////////////////////////////
			

			////Создаем каталог архива сайта
			

			@mkdir ( sfConfig::get ( 'sf_web_dir' ) . '/export/' . $this->name );
		
		}
	
	}
	
	
	
	public function copy_distrib($idd) {
		
		/// удаляем директорию дистрибутива
		$this->removeDir( sfConfig::get ( 'app_users_dir' ) . $this->getNameDistrib() );
		
		$d = DistribPeer::retrieveByPK($idd);
		//echo 'sd-'.$idd ;
		//die;
		///Создаем директории
		@mkdir ( sfConfig::get ( 'app_users_dir' ) . $this->getNameDistrib() );
		@mkdir ( sfConfig::get ( 'app_users_dir' ) . $this->getNameDistrib() . '/images' );
		@mkdir ( sfConfig::get ( 'app_users_dir' ) . $this->getNameDistrib() . '/css' );
		@mkdir ( sfConfig::get ( 'app_users_dir' ) . $this->getNameDistrib() . '/js' );
		@mkdir ( sfConfig::get ( 'app_users_dir' ) . $this->getNameDistrib() . '/cache' );
		@mkdir ( sfConfig::get ( 'app_users_dir' ) . $this->getNameDistrib() . '/templates' );
		@mkdir ( sfConfig::get ( 'app_users_dir' ) . $this->getNameDistrib() . '/cache/images' );
		@mkdir ( sfConfig::get ( 'app_users_dir' ) . $this->getNameDistrib() . '/cache/images/thumbnails' );
		
		$dirs = array ();
		$dirs [] = '/';
		$dirs [] = '/images';
		$dirs [] = '/css';
		$dirs [] = '/js';
		$dirs [] = '/cache';
		$dirs [] = '/templates';
		
		/// копируем
		foreach ( $dirs as $k => $v ) {
			
			if ($dir = @opendir ( sfConfig::get ( 'app_destrib_dir' ) . $d . $v )) {
				while ( ($file = readdir ( $dir )) !== false ) {
					
					if ($file != '..' and $file != '.' and $file != '.svn') {
						
						//echo $file.'<br/>';
						@copy ( sfConfig::get ( 'app_destrib_dir' ) . $d . $v . '/' . $file, sfConfig::get ( 'app_users_dir' ) . $this->getNameDistrib() . $v . '/' . $file );
					}
				}
			} else {
				echo 'не могу открыть директорию:' . sfConfig::get ( 'app_destrib_dir' ) . $d . $v;
				die ();
			}
		}
	
	}
	
	public function config_distrib() {
		
		
		$filename =  sfConfig::get ( 'app_users_dir' ) . $this->getNameDistrib() . '/config.php';
		
		///Переписываем конфигурационный файл
		copy ( sfConfig::get ( 'app_destrib_dir' ) . $this->getDistrib() . "/config.php", $filename);
		
		///парсим  конфигурационный файл 
		

		$fd = fopen ( $filename, "r" );
		$contents = fread ( $fd, filesize ( $filename ) );
		fclose ( $fd );
		$contents = str_replace ( '@main_domain@', sfConfig::get('app_main_domain'), $contents );
		$contents = str_replace ( '@name_domain@', $this->name, $contents );
		$contents = str_replace ( '@slogan1@', $this->slogan1, $contents );
		$contents = str_replace ( '@slogan2@', $this->slogan2, $contents );
		$contents = str_replace ( '@text_contact@', $this->text_contact, $contents );
		$contents = str_replace ( "'DEMO','true'", "'DEMO','false'", $contents );
		$contents = str_replace ( '@item_per_page@', $this->items_per_page, $contents );
		$contents = str_replace ( '@cache_time@', $this->cache_time, $contents );
		
		////Меню
		$contents = str_replace ( '@menu_mainpage@', $this->menu_mainpage, $contents );
		$contents = str_replace ( '@menu_service@', $this->menu_service, $contents );
		$contents = str_replace ( '@menu_articles@', $this->menu_articles, $contents );
		$contents = str_replace ( '@menu_contact@', $this->menu_contact, $contents );
		/////
		

		////SEO
		$contents = str_replace ( '@seo_title@', $this->seo_title, $contents );
		$contents = str_replace ( '@seo_description@', $this->seo_description, $contents );
		$contents = str_replace ( '@seo_keywords@', $this->seo_keywords, $contents );
		$contents = str_replace ( '@seo_code_counter@', $this->seo_code_counter, $contents );
		/////
		

		$fd = fopen ( $filename, "w" );
		fwrite ( $fd, $contents );
		fclose ( $fd );
		
		///парсим распаковщик 
		$filename = sfConfig::get ( 'app_users_dir' ) . $this->getNameDistrib() . '/extract.php';
		
		$fd = fopen ( $filename, "r" );
		$contents = fread ( $fd, filesize ( $filename ) );
		fclose ( $fd );
		$contents = str_replace ( '@site@', $this->getNameDistrib(), $contents );
		
		$fd = fopen ( $filename, "w" );
		fwrite ( $fd, $contents );
		fclose ( $fd );
		//////////////////////////
	

	}
	
	public function make_archive() {
		
		$archive_dir = sfConfig::get ( 'sf_web_dir' ) . "/export/" . $this->name;
		sfToolkit::clearDirectory($archive_dir);
		@mkdir($archive_dir);	
		$src_dir = sfConfig::get ( 'app_cms_dir' ) . $this->getNameDistrib() . '/';
		$archive = new PclZip ( $archive_dir . '/data.zip' );
		$v_list = $archive->add ( $src_dir );
		if ($v_list == 0) {
			die ( "Error : " . $archive->errorInfo ( true ) );
		}
	
	}
	
	/*
	 * Функция создания статичного кэша
	 */
	
	public function create_scache() {
		
		ini_set ( 'max_execution_time', 0 );
		
		////Главная страница
		$filename = sfConfig::get ( 'app_users_dir' ) . $this->getNameDistrib() . '/cache/main.htm';
		$fd = fopen ( $filename, "w" );
		fwrite ( $fd, $this->getMainPageText());
		fclose ( $fd );
		
	
		////Страница услуг
		$filename = sfConfig::get ( 'app_users_dir' ) . $this->getNameDistrib() . '/cache/service.htm';
		$fd = fopen ( $filename, "w" );
		fwrite ( $fd, $this->getServicePageText() );
		fclose ( $fd );
	
		
		return true;
	
	}
	
	public function CodeURL() {
		$str = str_replace ( '/', '-', $this->name );
		$str = str_replace ( '.', '-dot-', $this->name );
		return $str;
	}
	
public function getNameDistrib() {
		
		return $this->getDistrib().'_'.$this->name;
	}
	
   
}
